﻿using YooAssetsSystem.Fsm;
using UnityEngine;
using UnityEngine.SceneManagement;
using YooAsset;

namespace YooAssetsSystem.Patch
{
	/// <summary>
	/// 检查版本号 2
	/// </summary>
	public class UpdateVersionNode : BasePatchNode
	{
		public override void OnEnter()
		{
			Owner.Event.ChangeStateEvent?.Invoke("检查资源版本");
			var package = YooAssets.GetPackage(shareData.package);
			var operation = package.RequestPackageVersionAsync(true, 5);

			operation.Completed += delegate(AsyncOperationBase operationBase)
			{
				if (operation.Status == EOperationStatus.Succeed)
				{
					Debug.Log($"找到远端最新版本为: {operation.PackageVersion}");
					shareData.version = operation.PackageVersion;
					machine.ChangeState<UpdateManifestNode>();
				}
				else
				{
					Debug.LogError(operation.Error);
					Owner.Event.UpdateFailedEvent?.Invoke($"{GetType().Name}失败, 停止更新");
				}
			};
		}

		public override void OnExit()
		{
			base.OnExit();
		}
	}
}
